package com.android.djlmapp.token;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;

import com.android.djlmapp.activity.home.HomeActivity;
import com.android.djlmapp.activity.login.LoginActivity;
import com.android.djlmapp.server.RefreshService;
import com.android.djlmapp.tools.global.ConstantsValue;
import com.android.djlmapp.tools.global.MyActivityManager;
import com.android.djlmapp.tools.global.PrefUtils;
import com.android.djlmapp.tools.global.ToastUtils;
import com.android.djlmapp.tools.global.UserInfoUtils;

import java.util.List;
import java.util.Stack;

/**
 * web刷新token
 */

public class GetUrlStr {
    public static void getStrUrl(final Activity activity) {

        new WebTokenUtils(activity, new WebTokenUtils.RefreshTokenListener() {
            @Override
            public void refreshTokenSuc(String token) {
                ToastUtils.showShortToast(activity, "长时间未操作,请重新操作!");
                PrefUtils.putString( ConstantsValue.TOKEN, token);
                Context ctx = (Context) activity;
                boolean foreground = isForeground(ctx, HomeActivity.class.getName());
                boolean isLog =  UserInfoUtils.getUserInfoUtilsInstance().getIsLog();
                if (!foreground && isLog) {
                    Intent intent = new Intent(activity,
                            HomeActivity.class);
                    activity.startActivity(intent);
                    activity.finish();
                }
            }

            @Override
            public void refreshOnFail() {
                exitLogin(activity);
            }

            @Override
            public void refreshOnErr() {
                ToastUtils.showShortToast(activity, "网络或服务器错误");
            }
        }).refreshServ();
    }

    public static void exitLogin(Activity activity) {
        ToastUtils.showShortToast(activity, "登录过期,请重新登录");

        UserInfoUtils.getUserInfoUtilsInstance().setIsLog(false);
        UserInfoUtils.getUserInfoUtilsInstance().setToken("");
        activity.stopService(new Intent(activity, RefreshService.class));

        boolean foreground = isForeground(activity, LoginActivity.class.getName());
        Log.e("项目异常", "------TokenExpiredUtils---foreground------" + foreground);
        if (!foreground) {
            Stack<Activity> stack = MyActivityManager.mActivites;
            if (stack != null && stack.size() > 0) {
                Activity mActivity = stack.lastElement();
                Intent intent = new Intent(mActivity, LoginActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK
                        | Intent.FLAG_ACTIVITY_NEW_TASK);
                mActivity.startActivity(intent);
            }
        }
    }

    /**
     * 判断某个界面是否在前台
     *
     * @param context
     * @param className 某个界面名称
     */
    private static boolean isForeground(Context context, String className) {
        if (context == null || TextUtils.isEmpty(className)) {
            return false;
        }

        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> list = am.getRunningTasks(1);
        if (list != null && list.size() > 0) {
            ComponentName cpn = list.get(0).topActivity;
            if (className.equals(cpn.getClassName())) {
                return true;
            }
        }
        return false;
    }
}
